package com.boot.security.server.controller;

import java.awt.print.Book;
import java.util.*;
import java.util.stream.Collectors;

import com.boot.security.server.dto.appregmarkDto;
import com.boot.security.server.dto.appremkDto;
import com.boot.security.server.model.AppAccount;
import com.boot.security.server.model.appmarkmod;
import com.boot.security.server.utils.DateUtils;
import com.boot.security.server.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.AppRegsterMarkDao;
import com.boot.security.server.model.AppRegsterMark;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/appRegsterMarks")
public class AppRegsterMarkController {
    private static final Logger log = LoggerFactory.getLogger("adminLogger");
    @Autowired
    private AppRegsterMarkDao appRegsterMarkDao;

    @PostMapping
    @ApiOperation(value = "保存")
    public AppRegsterMark save(@RequestBody AppRegsterMark appRegsterMark) {
        appRegsterMarkDao.save(appRegsterMark);

        return appRegsterMark;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public AppRegsterMark get(@PathVariable Long id) {
        return appRegsterMarkDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public AppRegsterMark update(@RequestBody AppRegsterMark appRegsterMark) {
        appRegsterMarkDao.update(appRegsterMark);

        return appRegsterMark;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return appRegsterMarkDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<AppRegsterMark> list(PageTableRequest request) {
                return appRegsterMarkDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        appRegsterMarkDao.delete(id);
    }

    /**
     * 查询
     * @param appRegsterMark
     * 返回科室列表
     * @return R
     */
    @PostMapping("/find")
    public R findinfo(@RequestBody AppRegsterMark appRegsterMark){
   //取出单个医生

          List<AppRegsterMark>  appRegsterMarkList;
        List<appregmarkDto>  appremkDtoList;
          List<AppRegsterMark>  appRegsterMarkList1  = new ArrayList();
        List<AppRegsterMark>  appRegsterMarkList1s  = new ArrayList();
        AppRegsterMark appRegsterMarkse =new AppRegsterMark();


     //   appRegsterMarkList=appRegsterMarkDao.getBylist(appRegsterMark.getHostcode(),appRegsterMark.getDeptcode(),appRegsterMark.getDoctorcode());
        appremkDtoList=appRegsterMarkDao.getBydtolist(appRegsterMark.getHostcode(),appRegsterMark.getDeptcode(),appRegsterMark.getDoctorcode(),appRegsterMark.getDeptcode1(),appRegsterMark.getDeptcode2());
        AppRegsterMark appRegsterMarks=new AppRegsterMark();
      //  appRegsterMarkList1=appRegsterMarkList;
        List<appregmarkDto> unique = appremkDtoList.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(o -> o.getDoctorcode()))), ArrayList::new)
        );
        System.out.println(unique.toString());
        for (int i = 0; i < unique.size(); i++) {
            log.info("医生1-------- "+unique.get(i).getDoctorcode());
            List<appmarkmod>  appmarkmodList = new ArrayList();
            for (int j = 0; j < appremkDtoList.size(); j++) {

                if(appremkDtoList.get(j).getDoctorcode().equals(unique.get(i).getDoctorcode())){

                    appmarkmod appmarkmods=new appmarkmod();
                    log.info("医生1-------- 想的了"+appremkDtoList.get(j).getDoctorcode());
                    appmarkmods.setCount(appremkDtoList.get(j).getCount().toString());
                    appmarkmods.setDay(DateUtils.toLongYMD(appremkDtoList.get(j).getDay()));
                    appmarkmods.setReghalf(appremkDtoList.get(j).getReghalf());
                    appmarkmodList.add(appmarkmods);

                }

            }

            unique.get(i).setAppmarkmodList(appmarkmodList);

        }


//        for (int i = 0; i < appRegsterMarkList.size(); i++) {
//            //appRegsterMarkse
//            appRegsterMarkse=appRegsterMarkList.get(i);
//            //appRegsterMarkList1.contains()
//            log.info("医生1----------------------------------------："+appRegsterMarkse.getDoctorcode());
//            if (appRegsterMarkList1==null || appRegsterMarkList1.isEmpty()){
//                appRegsterMarkList1.add(appRegsterMarkse);
//            }
//            else {
//                for (int k = 0; k < appRegsterMarkList1.size(); k++) {
//                    log.info("医生1--------------appRegsterMarkList1--------------------------："+appRegsterMarkList1.get(k).getDoctorcode());
//                    log.info("医生1--------------appRegsterMarkList--------------------------："+appRegsterMarkList.get(i).getDoctorcode());
//                    if (appRegsterMarkList1.get(k).getDoctorcode()!=appRegsterMarkList.get(i).getDoctorcode()){
//                        log.info("医生新："+appRegsterMarkse.getDoctorcode());
//                        appRegsterMarkList1.add(appRegsterMarkse);
//                    }
//                }
//
//            }
//        }

//         List<appremkDto> appremkDtoList;
//        appRegsterMarkList= appRegsterMarkDao.getBylist(appRegsterMark.getHostcode(),appRegsterMark.getDeptcode(),appRegsterMark.getDoctorcode());
//        for (int i = 0; i < appRegsterMarkList.size(); i++) {
//            appremkDtoList=appRegsterMarkList;
//        }appRegsterMarkList
        return new R<>(unique);
        //    return new R<>(  bxbFbqksDao.getById(bxbFbqks.getId()));
    }

}
